<template>
  <div>
    <div class="title">
      <span class="iconfont icon-left gobackIcon" @click="()=>{this.$router.go(-1)}"></span>
      <p>商品管理</p>
      <span class="title_rightAddBtn" @click="()=>{this.$router.push('/postProductInfo')}">发布</span>
    </div>
    <div>
      <van-tabs v-model="active" line-height="2px" title-active-color="#0cbccb" color="#0cbccb">
        <van-tab v-for="(item,i) in tabitem" :key="i" :title="item">
          <div>
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
              <van-card v-for="item in commodityList" :key="item.id" style="padding:8px 0">
                <div slot="thumb" style="padding:0 10px;height:65px;">
                  <img :src="item.mainImage" alt style="width:100%;height:100%;" />
                </div>
                <div slot="title" class="titleSty">{{item.productName}}</div>
                <div slot="desc" class="descSty">
                  <span class="name">零售价:</span>
                  <span class="value">￥{{item.retailPrice}}</span>
                </div>
                <div slot="price" class="priceSty">
                  <span class="name">库存:</span>
                  <span class="value">{{item.stock}}</span>
                </div>
                <div slot="footer" class="footerBtn" v-if="active === 0 || active === 1">
                  <ul>
                    <li>
                      <i class="iconfont icon-bianji1 icons"></i>
                      <span>编辑</span>
                    </li>
                    <li @click="deleteBusiness(item)">
                      <i class="iconfont icon-shanchu icons"></i>
                      <span>删除</span>
                    </li>
                    <li @click="takeOff(item)">
                      <i :class="['iconfont',active===1?'icon-shangjia':'icon-xiajia','icons']"></i>
                      <span>{{active ===1?'上架':'下架'}}</span>
                    </li>
                  </ul>
                </div>
                <div slot="footer" class="revoke" v-if="active === 2">
                  <ul>
                    <li>
                      <i class="iconfont icon-weibiaoti545 icons"></i>
                      <span>撤销</span>
                    </li>
                  </ul>
                </div>
                <div slot="footer" class="fail" v-if="active === 3">
                  <span>审核意见：{{item.auditOpinion}}</span>
                  <ul>
                    <li @click="()=>{$router.push('/postProductInfo')}">
                      <i class="iconfont icon-bianji2 icons"></i>
                      <span>编辑</span>
                    </li>
                    <li @click="deleteBusiness(item)">
                      <i class="iconfont icon-shanchu icons"></i>
                      <span>删除</span>
                    </li>
                  </ul>
                </div>
              </van-card>
            </van-list>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<script>
export default {
  data: () => ({
    active: 0,
    limit: 5,
    page: 1,
    loading: false,
    finished: false,
    commodityList: [],
    tabitem: ["出售中", "已下架", "审核中", "审核不通过"]
  }),
  created() {},
  watch: {
    active(newVlue) {
      this.commodityList = [];
      this.loading = true;
      this.finished = false;
      this.limit = 5;
      this.page = 1;
      this.getCommdityList(newVlue + 1);
    }
  },
  methods: {
    takeOff(item) {
      let data = {
        id: item.id,
        salesStatus: this.active === 0 ? 0 : 1
      };
      console.log(data);
      this.$axios
        .post(`ym/online-merchant-product/modify-sales-status`, data)
        .then(res => {
          if (res.data.code === 0) {
            this.commodityList.forEach((msg, index) => {
              if (item.id === msg.id) {
                this.commodityList.splice(index, 1);
              }
            });
            if (this.active === 0) {
              this.$Toast("下架成功！");
            } else {
              this.$Toast("上架成功！");
            }
          } else {
            if (this.active === 0) {
              this.$Toast("下架失败！");
            } else {
              this.$Toast("上架失败！");
            }
          }
          console.log(res);
        });
    },
    deleteBusiness(item) {
      let event = {
        id: item.id
      };
      this.$axios.post(`ym/online-merchant-product/del`, event).then(res => {
        if (res.data.code === 0) {
          this.commodityList.forEach((msg, inx) => {
            if (msg.id == item.id) {
              this.commodityList.splice(inx, 1);
            }
          });
          this.$Toast("删除成功！");
        } else {
          this.$Toast("删除失败！");
        }
      });
    },
    getCommdityList(status) {
      this.$axios
        .get(
          `ym/online-merchant-product/list?status=${status}&limit=${this.limit}&page=${this.page}`
        )
        .then(res => {
          if (res.code === 0) {
            let pageInfo = res.data.records;
            if (pageInfo.length > 0) {
              pageInfo.forEach(msg => {
                this.commodityList.push(msg);
              });
              this.limit += 5;
              this.page += 1;
              this.loading = false;
            } else {
              this.finished = true;
            }
          } else {
            console.log(res.msg);
          }
        });
    },
    onLoad() {
      this.getCommdityList(this.active + 1);
    }
  }
};
</script>
<style lang="scss" scoped>
.title {
  margin: 0;
  padding: 10px 0;
  background: #0cbccb;
  color: #fff;
  .gobackIcon {
    border: 1px solid #0cbccb;
    position: absolute;
    left: 10px;
  }
  p {
    margin: 0;
    padding: 0;
    text-align: center;
  }
  .title_rightAddBtn {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 14px;
  }
}
.titleSty {
  color: #0cbccb;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.descSty {
  margin-top: 16px;
  .name {
    color: #0cbccb;
  }
  .value {
    font-size: 14px;
    color: #fe0000;
  }
}
.priceSty {
  font-size: 12px;
  color: #0cbccb;
  margin: 5px 0;
}
.footerBtn {
  border: 1px solid #ccc;
  padding: 5px 0;
  color: #666;
  ul {
    display: flex;
    flex-direction: row;
    li {
      width: 33.3%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      .icons {
        margin: 0 5px;
        font-size: 20px;
      }
    }
  }
}
.revoke {
  padding: 5px 0;
  font-size: 14px;
  color: #666;
  ul {
    border: 1px solid #ccc;
    height: 30px;
    li {
      display: flex;
      align-items: center;
      justify-content: center;
      position: absolute;
      right: 10px;
      .icons {
        margin: 0 5px;
        font-size: 25px;
      }
      span {
        padding-right: 10px;
      }
    }
  }
}
.fail {
  border: 1px solid #ccc;
  padding: 5px 0;
  color: #666;
  text-align: left;
  display: flex;
  > span {
    width: 65%;
    color: #c23232;
    font-size: 12px;
  }
  ul {
    display: flex;
    flex-direction: row;
    width: 35%;
    li {
      width: 50%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      .icons {
        padding: 0 3px;
        font-size: 20px;
      }
      span {
        margin: 0;
        padding: 0;
      }
    }
  }
}
</style>